﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace iHouse.Common.Membership
{
    public class RoleService:IRoleService
    {
        private readonly RoleProvider _provider;

        public RoleService():this(null)
        {
        }

        public RoleService(RoleProvider provider)
        {
            _provider = provider ?? Roles.Provider;
        }

        #region IRoleService Members

        public IEnumerable<string> FindAll()
        {
            return _provider.GetAllRoles();
        }

        public IEnumerable<string> FindByUserName(string userName)
        {
            return _provider.GetRolesForUser(userName);
        }

        public IEnumerable<string> FindUserNamesByRole(string roleName)
        {
            return _provider.GetUsersInRole(roleName);
        }

        public void AddToRole(System.Web.Security.MembershipUser user, string roleName)
        {
            _provider.AddUsersToRoles(new[] { user.UserName }, new[] { roleName });
        }

        public void RemoveFromRole(System.Web.Security.MembershipUser user, string roleName)
        {
            _provider.RemoveUsersFromRoles(new[] { user.UserName }, new[] { roleName });
        }

        public bool IsInRole(string userName, string roleName)
        {
            return _provider.IsUserInRole(userName, roleName);
        }

        public void Create(string roleName)
        {
            _provider.CreateRole(roleName);
        }

        public void Delete(string roleName)
        {
            _provider.DeleteRole(roleName, true);
        }

        public bool RoleExists(string roleName)
        {
            return _provider.RoleExists(roleName);
        }

        #endregion
    }
}
